﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Text;

namespace EXAM
{
    public partial class User : System.Web.UI.Page
    {

        String _sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserGuest"] != null && (txtID.Value == "" || txtID.Value == "-1"))
            {
                DTO.UserInfo usrinf = (DTO.UserInfo)Session["UserGuest"];
                SetDataToForm(usrinf);
            }
            else if (Session["UserGuest"] == null)
            {
                Response.Redirect("login.aspx");
            }
        }

        private DTO.UserInfo GetDataFromForm(DTO.UserInfo usrInf)
        {
            usrInf = null;
            if (Session["UserGuest"] != null)
            {
                usrInf = (DTO.UserInfo)Session["UserGuest"];

                if (txtID.Value != "" && txtID.Value != "-1")
                    usrInf.ID = Convert.ToInt64(txtID.Value);    

                //    usrInf.Ip = txtIP.Text;

            }
            return usrInf;
        }

        private void SetDataToForm(DTO.UserInfo usrInf)
        {
            if (Session["UserGuest"] != null)
            {
                usrInf = (DTO.UserInfo)Session["UserGuest"];

            }

            if (usrInf.ID != -1)
                txtID.Value = usrInf.ID.ToString();           
            
        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            DTO.UserInfo usrInf = new DTO.UserInfo();
            usrInf = GetDataFromForm(usrInf);
            if (txtconfirmpassword.Text != txtnewpassword.Text)
            {
                lbErr.Text = "Not exact";
                return;
            }
            string newpass= Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(txtnewpassword.Text)));
            string oldpass = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(txtPassword.Text)));

            if (oldpass != usrInf.Password)
            {
                lbErr.Text = "Incorrect Password";
                return;
            }
            usrInf.Password = newpass;
            BUS.UserControl ctr = new BUS.UserControl();
            if (txtID.Value != "" && txtID.Value != "-1")
                _sErr = ctr.Update_User(usrInf);

        }

        protected void txtconfirmpassword_TextChanged(object sender, EventArgs e)
        {
            if (txtnewpassword.Text != txtconfirmpassword.Text)
            {
                btSave.Enabled = false;
                lbErr.Text = "Not exact";
            }
            else
            {
                btSave.Enabled = true;
                lbErr.Text = "";
            }
        }

       
    }
}
